package co.gov.medellin.ssti.sub.service;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

import javax.naming.NamingException;

import co.gov.medellin.ssti.sub.common.ConexionFactory;
import co.gov.medellin.ssti.sub.model.dao.DAOFactory;
import co.gov.medellin.ssti.sub.model.dao.IAtributosDAO;
import co.gov.medellin.ssti.sub.model.dao.exception.DAOException;
import co.gov.medellin.ssti.sub.model.vo.AtributoVO;

/**
 * Clase encargada de acceder a los DAO y manejar las conexiones, para evitar
 * manejarlas directamente en las acciones de struts.
 * 
 * @author Consultsoft4
 * 
 */
public class AtributosService {

	/**
	 * Metodo que se encarga de obtener y liberar una conexion a base de datos
	 * con la cual llama
	 * {@link co.gov.medellin.ssti.sub.model.dao.IAtributosDAO#findAtributosSubprograma(Long, String, int)}
	 * para retornar una Collection de atributos.
	 * 
	 * @param codigoSubprograma
	 *            Codigo del subprograma.
	 * @param vigencia
	 *            "I": inactivos, "A": activos.
	 * @param opcion
	 * @return Collection de
	 *         {@link co.gov.medellin.ssti.sub.model.vo.AtributoVO}
	 */
	public Collection<AtributoVO> findAtributosSubprograma(
			String codigoSubprograma, String vigencia, int opcion)
			throws DAOException {
		IAtributosDAO atributosDAO = null;
		Connection con = null;
		Long codeSubprograma;

		codeSubprograma = new Long(codigoSubprograma);

		try {
			con = ConexionFactory.getInstance().getConnection();
			atributosDAO = DAOFactory.getDAOFactory(DAOFactory.ORACLE)
					.getAtributosDAO(con);
			Collection<AtributoVO> atributos = atributosDAO
					.findAtributosSubprograma(codeSubprograma, vigencia, opcion);
			return atributos;
		} catch (Exception e) {
			throw new DAOException(e);
		} finally {
			if (atributosDAO != null) {
				if (!atributosDAO.isConnClosed()) {
					atributosDAO.close();
				}
			}
		}
	}

	/**
	 * Metodo que se encarga de obtener y liberar una conexion a base de datos
	 * con la cual llama
	 * {@link co.gov.medellin.ssti.sub.model.dao.IAtributosDAO#findAtributoEGSubprograma(Long)}
	 * para retornar el atributo de tipo 'GESTOR' para el subprograma.
	 * 
	 * @param codigoSubprograma
	 *            Codigo del subprograma.
	 * @return el {@link co.gov.medellin.ssti.sub.model.vo.AtributoVO}
	 * @throws DAOException
	 */
	public AtributoVO findAtributoEGSubprograma(String codigoSubprograma)
			throws DAOException {
		AtributoVO atributo;
		Connection con = null;
		IAtributosDAO atributosDAO = null;
		Long codeSubprograma;

		codeSubprograma = new Long(codigoSubprograma);
		try {
			con = ConexionFactory.getInstance().getConnection();
			atributosDAO = DAOFactory.getDAOFactory(DAOFactory.ORACLE)
					.getAtributosDAO(con);
			atributo = atributosDAO.findAtributoEGSubprograma(codeSubprograma);
			return atributo;
		} catch (Exception e) {
			throw new DAOException(e);
		} finally {
			if (atributosDAO != null) {
				if (!atributosDAO.isConnClosed()) {
					atributosDAO.close();
				}
			}
		}
	}

	/**
	 * 
	 * @param codigoAtributo
	 * @param vigencia
	 * @return
	 * @throws DAOException
	 */
	public AtributoVO findAtributoByCodigoAndVigencia(String codigoAtributo,
			String vigencia) throws DAOException {
		IAtributosDAO atributosDAO = null;
		try {
			Connection con = ConexionFactory.getInstance().getConnection();
			atributosDAO = DAOFactory.getDAOFactory(DAOFactory.ORACLE)
					.getAtributosDAO(con);
			return atributosDAO.findAtributoByCodigoAndVigencia(new Long(
					codigoAtributo), vigencia);
		} catch (NamingException e) {
			throw new DAOException(e);
		} catch (SQLException e) {
			throw new DAOException(e);
		} finally {
			if (atributosDAO != null) {
				if (!atributosDAO.isConnClosed()) {
					atributosDAO.close();
				}
			}
		}
	}

	/**
	 * 
	 * @param codigoAtributo
	 * @return
	 * @throws DAOException
	 */
	public AtributoVO findAtributoByCodigo(Long codigoAtributo)
			throws DAOException {
		IAtributosDAO atributosDAO = null;
		try {
			Connection con = ConexionFactory.getInstance().getConnection();
			atributosDAO = DAOFactory.getDAOFactory(DAOFactory.ORACLE)
					.getAtributosDAO(con);
			return atributosDAO.findAtributoByCodigo(codigoAtributo);
		} catch (NamingException e) {
			throw new DAOException(e);
		} catch (SQLException e) {
			throw new DAOException(e);
		} finally {
			if (atributosDAO != null) {
				if (!atributosDAO.isConnClosed()) {
					atributosDAO.close();
				}
			}
		}
	}

	/**
	 * 
	 * @return
	 * @throws DAOException
	 */
	public int findMaxCodigoRestriccion() throws DAOException {
		IAtributosDAO atributosDAO = null;
		try {
			Connection con = ConexionFactory.getInstance().getConnection();
			atributosDAO = DAOFactory.getDAOFactory(DAOFactory.ORACLE)
					.getAtributosDAO(con);
			return atributosDAO.findMaxCodigoRestriccion();
		} catch (NamingException e) {
			throw new DAOException(e);
		} catch (SQLException e) {
			throw new DAOException(e);
		} finally {
			if (atributosDAO != null) {
				if (!atributosDAO.isConnClosed()) {
					atributosDAO.close();
				}
			}
		}
	}

	/**
	 * WTF IS THIS SHIT.
	 * 
	 * @param codeSubprograma
	 * @return
	 * @throws SQLException
	 */
	public int findAtributosValidarLista(Long codeSubprograma)
			throws DAOException {
		IAtributosDAO atributosDAO = null;
		try {
			Connection con = ConexionFactory.getInstance().getConnection();
			atributosDAO = DAOFactory.getDAOFactory(DAOFactory.ORACLE)
					.getAtributosDAO(con);
			return atributosDAO.findAtributosValidarLista(codeSubprograma);
		} catch (NamingException e) {
			throw new DAOException(e);
		} catch (SQLException e) {
			throw new DAOException(e);
		} finally {
			if (atributosDAO != null) {
				if (!atributosDAO.isConnClosed()) {
					atributosDAO.close();
				}
			}
		}
	}

	/**
	 * 
	 * @param strValor
	 * @param strTipodato
	 * @param codeAtributo
	 * @param codeBeneficiario
	 * @param i
	 * @param strArrIns
	 * @return
	 * @throws DAOException
	 */
	public int insertBeneficiarioAtri(String strValor, String strTipodato,
			Long codeAtributo, Long codeBeneficiario, int i, String strArrIns[])
			throws DAOException {
		IAtributosDAO atributosDAO = null;
		try {
			Connection con = ConexionFactory.getInstance().getConnection();
			atributosDAO = DAOFactory.getDAOFactory(DAOFactory.ORACLE)
					.getAtributosDAO(con);
			return atributosDAO.insertBeneficiarioAtri(strValor, strTipodato,
					codeAtributo, codeBeneficiario, i, strArrIns);
		} catch (NamingException e) {
			throw new DAOException(e);
		} catch (SQLException e) {
			throw new DAOException(e);
		} finally {
			if (atributosDAO != null) {
				if (!atributosDAO.isConnClosed()) {
					atributosDAO.close();
				}
			}
		}
	}

	/**
	 * 
	 * @param strValor
	 * @param strTipodato
	 * @param codeAtri
	 * @param codeBot
	 * @param i
	 * @param strArrIns
	 * @return
	 * @throws DAOException
	 */
	public int insertBeneficioAtri(String strValor, String strTipodato,
			Long codeAtri, Long codeBot, int i, String strArrIns[])
			throws DAOException {
		IAtributosDAO atributosDAO = null;
		try {
			Connection con = ConexionFactory.getInstance().getConnection();
			atributosDAO = DAOFactory.getDAOFactory(DAOFactory.ORACLE)
					.getAtributosDAO(con);
			return atributosDAO.insertBeneficioAtri(strValor, strTipodato,
					codeAtri, codeBot, i, strArrIns);
		} catch (NamingException e) {
			throw new DAOException(e);
		} catch (SQLException e) {
			throw new DAOException(e);
		} finally {
			if (atributosDAO != null) {
				if (!atributosDAO.isConnClosed()) {
					atributosDAO.close();
				}
			}
		}
	}

	/**
	 * 
	 * @param strValor
	 * @param strTipodato
	 * @param codeAtri
	 * @param codeBot
	 * @param i
	 * @param strArrIns
	 * @return
	 * @throws DAOException
	 */
	public int updateBeneficioAtri(String strValor, String strTipodato,
			Long codeAtri, Long codeBot, int i, String strArrIns[])
			throws DAOException {
		IAtributosDAO atributosDAO = null;
		try {
			Connection con = ConexionFactory.getInstance().getConnection();
			atributosDAO = DAOFactory.getDAOFactory(DAOFactory.ORACLE)
					.getAtributosDAO(con);
			return atributosDAO.updateBeneficioAtri(strValor, strTipodato,
					codeAtri, codeBot, i, strArrIns);
		} catch (NamingException e) {
			throw new DAOException(e);
		} catch (SQLException e) {
			throw new DAOException(e);
		} finally {
			if (atributosDAO != null) {
				if (!atributosDAO.isConnClosed()) {
					atributosDAO.close();
				}
			}
		}
	}

	/**
	 * 
	 * @param arr
	 * @return
	 * @throws DAOException
	 */
	public int insertRestriccion(String arr[]) throws DAOException {
		IAtributosDAO atributosDAO = null;
		try {
			Connection con = ConexionFactory.getInstance().getConnection();
			atributosDAO = DAOFactory.getDAOFactory(DAOFactory.ORACLE)
					.getAtributosDAO(con);
			return atributosDAO.insertRestriccion(arr);
		} catch (NamingException e) {
			throw new DAOException(e);
		} catch (SQLException e) {
			throw new DAOException(e);
		} finally {
			if (atributosDAO != null) {
				if (!atributosDAO.isConnClosed()) {
					atributosDAO.close();
				}
			}
		}
	}

	/**
	 * 
	 * @param codeSubprograma
	 * @param codeAtri
	 * @return
	 * @throws DAOException
	 */
	public int activarAtributo(Long codeSubprograma, Long codeAtri)
			throws DAOException {
		IAtributosDAO atributosDAO = null;
		try {
			Connection con = ConexionFactory.getInstance().getConnection();
			atributosDAO = DAOFactory.getDAOFactory(DAOFactory.ORACLE)
					.getAtributosDAO(con);
			return atributosDAO.activarAtributo(codeSubprograma, codeAtri);
		} catch (NamingException e) {
			throw new DAOException(e);
		} catch (SQLException e) {
			throw new DAOException(e);
		} finally {
			if (atributosDAO != null) {
				if (!atributosDAO.isConnClosed()) {
					atributosDAO.close();
				}
			}
		}
	}

	/**
	 * 
	 * @param codeSubprograma
	 * @param codeAtri
	 * @param codeMaxRest
	 * @param strArrAct
	 * @return
	 * @throws DAOException
	 */
	public int updateCampoRestAtri(Long codeSubprograma, Long codeAtri,
			Long codeMaxRest, String strArrAct[]) throws DAOException {
		IAtributosDAO atributosDAO = null;
		try {
			Connection con = ConexionFactory.getInstance().getConnection();
			atributosDAO = DAOFactory.getDAOFactory(DAOFactory.ORACLE)
					.getAtributosDAO(con);
			return atributosDAO.updateCampoRestAtri(codeSubprograma, codeAtri,
					codeMaxRest, strArrAct);
		} catch (NamingException e) {
			throw new DAOException(e);
		} catch (SQLException e) {
			throw new DAOException(e);
		} finally {
			if (atributosDAO != null) {
				if (!atributosDAO.isConnClosed()) {
					atributosDAO.close();
				}
			}
		}
	}

	/**
	 * 
	 * @param codeSubprog
	 * @return
	 * @throws DAOException
	 */
	public List<AtributoVO> findAtributosSAB(Long codeSubprog)
			throws DAOException {
		IAtributosDAO atributosDAO = null;
		try {
			Connection con = ConexionFactory.getInstance().getConnection();
			atributosDAO = DAOFactory.getDAOFactory(DAOFactory.ORACLE)
					.getAtributosDAO(con);
			return atributosDAO.findAtributosSAB(codeSubprog);
		} catch (NamingException e) {
			throw new DAOException(e);
		} catch (SQLException e) {
			throw new DAOException(e);
		} finally {
			if (atributosDAO != null) {
				if (!atributosDAO.isConnClosed()) {
					atributosDAO.close();
				}
			}
		}
	}

	/**
	 * 
	 * @param codeAtri
	 * @return
	 * @throws DAOException
	 */
	public String findNombreAtributoSAB(Long codeAtri) throws DAOException {
		IAtributosDAO atributosDAO = null;
		try {
			Connection con = ConexionFactory.getInstance().getConnection();
			atributosDAO = DAOFactory.getDAOFactory(DAOFactory.ORACLE)
					.getAtributosDAO(con);
			return atributosDAO.findNombreAtributoSAB(codeAtri);
		} catch (NamingException e) {
			throw new DAOException(e);
		} catch (SQLException e) {
			throw new DAOException(e);
		} finally {
			if (atributosDAO != null) {
				if (!atributosDAO.isConnClosed()) {
					atributosDAO.close();
				}
			}
		}
	}

	/**
	 * 
	 * @param strIncrement
	 * @return
	 * @throws DAOException
	 */
	public String findFechaBeneficio(String strIncrement) throws DAOException {
		IAtributosDAO atributosDAO = null;
		try {
			Connection con = ConexionFactory.getInstance().getConnection();
			atributosDAO = DAOFactory.getDAOFactory(DAOFactory.ORACLE)
					.getAtributosDAO(con);
			return atributosDAO.findFechaBeneficio(strIncrement);
		} catch (NamingException e) {
			throw new DAOException(e);
		} catch (SQLException e) {
			throw new DAOException(e);
		} finally {
			if (atributosDAO != null) {
				if (!atributosDAO.isConnClosed()) {
					atributosDAO.close();
				}
			}
		}
	}

	/**
	 * Metodo que consulta el codigo del atributo generico a partir del nombre
	 * en TCS_BEN_ATR_GENERICO.
	 * 
	 * @param nombreAtri
	 *            Nombre del atributo generico.
	 * @return Codigo del atributo generico.
	 * @throws DAOException
	 */
	public String findCodeAtributoGenerico(String nombreAtri)
			throws DAOException {
		IAtributosDAO atributosDAO = null;
		try {
			Connection con = ConexionFactory.getInstance().getConnection();
			atributosDAO = DAOFactory.getDAOFactory(DAOFactory.ORACLE)
					.getAtributosDAO(con);
			return atributosDAO.findCodeAtributoGenerico(nombreAtri);
		} catch (NamingException e) {
			throw new DAOException(e);
		} catch (SQLException e) {
			throw new DAOException(e);
		} finally {
			if (atributosDAO != null) {
				if (!atributosDAO.isConnClosed()) {
					atributosDAO.close();
				}
			}
		}
	}

	/**
	 * Metodo que actualiza la columna VALIDAR_LISTA y VALOR_SUGERIDO de
	 * TCS_BEN_SUBP_ATR_BENEFICIARIO a 'N' y '' respectivamente, del registro
	 * con CODIGO = codeAtri.
	 * 
	 * @param codeAtri
	 *            Codigo del atributo del beneficio.
	 * @return Numero de registros actualizados. 0 si no realiza la
	 *         actualizacion.
	 * @throws DAOException
	 */
	public int updateValidarLista(Long codeAtri) throws DAOException {
		IAtributosDAO atributosDAO = null;
		try {
			Connection con = ConexionFactory.getInstance().getConnection();
			atributosDAO = DAOFactory.getDAOFactory(DAOFactory.ORACLE)
					.getAtributosDAO(con);
			return atributosDAO.updateValidarLista(codeAtri);
		} catch (NamingException e) {
			throw new DAOException(e);
		} catch (SQLException e) {
			throw new DAOException(e);
		} finally {
			if (atributosDAO != null) {
				if (!atributosDAO.isConnClosed()) {
					atributosDAO.close();
				}
			}
		}
	}
}
